<template>
  <div>
    <el-input
      v-model="filterText"
      placeholder="输入关键字进行过滤"/>
    <el-tree ref="tree" :data="subjects" :props="defaultProps" :filter-node-method="filterNode" default-expand-all/>
  </div>
</template>
<script>
import subjectApi from '@/api/subject'

export default {
  data() {
    return {
      subjects: [],
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getSubjects()
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.title.toUpperCase().indexOf(value.toUpperCase()) !== -1
    },

    getSubjects() {
      subjectApi.getSubjectNestedList().then(r => {
        this.subjects = r.data.items
        // console.log(this.subjects)
      })
    }
  }
}
</script>
